package com.pay.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pay.order.entity.PayRateSearchDTO;
import com.pay.order.entity.ResponseResult;
import com.pay.order.pojo.PayRate;
import com.pay.order.service.PayRateService;
import com.pay.order.utils.PageDataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/payRate")
public class PayRateController {
    @Autowired
    private PayRateService payRateService;

    /**
     * 费率页面
     * @return
     */
    @RequestMapping("/index")
    public String toMemberList() {
        return "payRate/index";
    }
    @RequestMapping("/create")
    @ResponseBody
    public ResponseResult create(@RequestBody PayRate payRate){
        payRateService.save(payRate);
        return  new ResponseResult();
    }
    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult update(@RequestBody PayRate payRate){
        payRateService.updateById(payRate);
        return  new ResponseResult();
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public ResponseResult delete(@PathVariable("id") Integer id){
        payRateService.removeById(id);
        return  new ResponseResult();
    }
    @RequestMapping("/selectPage")
    @ResponseBody
    public PageDataResult searchList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, PayRateSearchDTO payRate){
        if (null == limit) {
            limit = 10;
        }
        if (null == page) {
            page = 0;
        }else {
            page=(page-1)*limit;
        }
        Page<PayRate> pageInfo = payRateService.queryPageList(payRate,page,limit);
        PageDataResult result = new PageDataResult();
        result.setCode(200);
        result.setTotals((int) pageInfo.getTotal());
        result.setList(pageInfo.getRecords());
        return result;
    }

    @RequestMapping("/get/{id}")
    @ResponseBody
    public ResponseResult<PayRate> searchList(@PathVariable("id") Integer id){

        PayRate payRate = payRateService.getById(id);
        ResponseResult result = new ResponseResult();
        result.setObj(payRate);
        return result;
    }

    @RequestMapping("/selectList")
    @ResponseBody
    public ResponseResult selectList(){
        List<PayRate> list = payRateService.list();
        ResponseResult result = new ResponseResult();
        result.setObj(list);
        return  result;
    }
}
